<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="会员账号、会员名称、商品名称" clearable style="width: 280px"></el-input>
      </el-form-item>
      <el-form-item label="">
				<el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="起始时间">
			  </el-date-picker>
			</el-form-item>
			<el-form-item label="">
				<el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间">
			  </el-date-picker>
			</el-form-item>
      <el-form-item label="商品状态">
				<el-select v-model="dataForm.goodStatues" placeholder="商品状态">
					<el-option label="全部" value="" ></el-option>
					<el-option label="未审核" value="0" ></el-option>
					<el-option label="审核通过" value="1" ></el-option>
					<el-option label="审核未通过" value="-1" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item label="商品类型">
				<el-select v-model="dataForm.goodType" placeholder="商品类型">
					<el-option label="全部" value="" ></el-option>
					<el-option label="普通商品" value="0" ></el-option>
					<el-option label="抵扣券商品" value="1" ></el-option>
					<el-option label="体验券商品" value="2" ></el-option>
          <el-option label="兑换商品" value="3" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="
          dataForm.keywords = ''
          dataForm.startTime = ''
          dataForm.endTime = ''
          dataForm.goodStatues = ''
          dataForm.goodType = ''
          getDataList()">重置</el-button>
        <!-- <el-button v-if="isAuth('goods:shoppingbusinessgoods:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('goods:shoppingbusinessgoods:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
        <el-badge :value="this.num" class="item">
          <el-button size="small" @click="unaudited()">未审核</el-button>
        </el-badge>
        <el-form-item label="" style="margin-left:50px">
				<el-select v-model="dataForm.batch" placeholder="批量审核">
					<el-option label="审核通过" value="1" ></el-option>
          <el-option label="审核不通过" value="-1" ></el-option>
				</el-select>
        <el-button v-if="isAuth('business:goods:modifyBatchIds')" type="danger" @click="batch()" :disabled="dataListSelections.length <= 0">批量审核</el-button>
			</el-form-item>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="mainImg"
        header-align="center"
        align="center"
        label="商品图片">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover">
            <img :src="scope.row.mainImg"/>
            <img slot="reference" :src="scope.row.mainImg" :alt="scope.row.mainImg" style="max-height: 50px;max-width: 130px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsName"
        header-align="center"
        align="center"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="trueName"
        header-align="center"
        align="center"
        label="商家名称">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="商家账号">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="marketPrice"
        header-align="center"
        align="center"
        label="市场价">
        <template slot-scope="scope">
          <span v-if="scope.row.marketPrice != null">￥ {{scope.row.marketPrice}}</span>
          <span v-if="scope.row.marketPrice == null">￥ 0</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="vipPrice"
        header-align="center"
        align="center"
        label="会员价">
        <template slot-scope="scope">
          <span v-if="scope.row.vipPrice != null">￥ {{scope.row.vipPrice}}</span>
          <span v-if="scope.row.vipPrice == null">￥ 0</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="commission"
        header-align="center"
        align="center"
        label="现金券">
      </el-table-column>
      <el-table-column
        prop="clickCount"
        header-align="center"
        align="center"
        label="点击数量">
      </el-table-column>
      <el-table-column
        prop="saleCount"
        header-align="center"
        align="center"
        label="销售量">
      </el-table-column>
      <el-table-column
        prop="goodsStock"
        header-align="center"
        align="center"
        label="库存">
      </el-table-column>
      <el-table-column
        prop="distributionTypes"
        header-align="center"
        align="center"
        label="配送方式">
        <template slot-scope="scope">
            <p v-if="scope.row.distributionTypes == '-1'"> 物流、自取、商家配送</p>
            <p v-else-if ="scope.row.distributionTypes == '0'">物流</p>
            <p v-else-if ="scope.row.distributionTypes == '1'">自取</p>
            <p v-else-if ="scope.row.distributionTypes == '2'">商家配送</p>
        </template>
      </el-table-column>
      <el-table-column
        prop="deliverMoney"
        header-align="center"
        align="center"
        label="商品运费">
      </el-table-column>
      <el-table-column
        prop="saleTime"
        header-align="center"
        align="center"
        label="上架时间">
      </el-table-column>
      <el-table-column
        prop="shelvesTime"
        header-align="center"
        align="center"
        label="下架时间">
      </el-table-column>
      <el-table-column
        prop="goodsStatus"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
            <p v-if="scope.row.goodsStatus == '-1'"> 禁售</p>
            <p v-else-if ="scope.row.goodsStatus == '0'">待审核</p>
            <p v-else-if ="scope.row.goodsStatus == '1'">审核通过</p>
        </template>
      </el-table-column>
      <el-table-column
      prop="goodsStatus"
      header-align="center"
      align="center"
      label="审核操作"
      width="100">
        <template slot-scope="scope">
        <!--  <el-button size="small" type="warning" icon="check" @click="adopt(scope.row.goodsId)" v-if="scope.row.goodsStatus== 0">通过</el-button>
          <el-button size="small" type="danger" icon="edit" @click="noAdopt(scope.row.goodsId)" v-if="scope.row.goodsStatus== 0">不通过</el-button>
          <el-button size="small" type="warning" icon="check" @click="goodsAdopt(scope.row.goodsId)" v-if="scope.row.goodsStatus != 0">重新审核</el-button>-->
          <div style="margin: 10px auto auto -10px">
            <el-button size="small" style="background: #6CDDD1; width: 50px; color:#000000"  @click="adopt(scope.row.goodsId)" v-if="(scope.row.goodsStatus)== 0">通过</el-button><br/>
            <el-button size="small" style="background: #F56C6C; width: 50px;margin-top: 5px; color:#000000"  @click="noAdopt(scope.row.goodsId)" v-if="(scope.row.goodsStatus)== 0">拒绝</el-button>
            <!-- <p v-if="scope.row.status == 1">审核通过</p>
            <p v-if="scope.row.status == 2">审核不通过</p>
            <p v-if="scope.row.status == 3">资料待完善</p>
            <p v-if="scope.row.status == 5">被锁定</p>
            <p v-if="scope.row.status == 7">注册了但未登陆</p> -->
            <el-button size="small" style="background: #EBB563; width: 77px; color:#000000; margin: 5px 0px 30px 0px" icon="check" @click="goodsAdopt(scope.row.goodsId)" v-if="(scope.row.goodsStatus) !== 0">重新审核</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-popconfirm
            title="查看？">
            <el-button slot="reference" size="small" @click="addOrUpdateHandle(scope.row.goodsId)">查看</el-button>
          </el-popconfirm>
          <el-popconfirm>
            <!-- <el-button type="text" size="small" @click="deleteHandle(scope.row.goodsid)">删除</el-button> -->
            <el-button slot="reference" size="small" @click="issale(scope.row.goodsId)" v-if="scope.row.isSale == 0">上架</el-button>
            <el-button slot="reference" size="small" @click="noIssale(scope.row.goodsId)" v-if="scope.row.isSale == 1">下架</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <goods-adopt v-if="addOrUpdateVisible" ref="goodsAdopt" @refreshDataList="getDataList"></goods-adopt>
  </div>
</template>

<script>
  import AddOrUpdate from './shoppingbusinessgoods-add-or-update'
  import goodsAdopt from './goods-adopt'
  export default {
    data () {
      return {
        dataForm: {
          keywords: '',
          lastStart: '',
          lastEnd: '',
          goodStatues: '',
          goodType: '',
          batch: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        num: ''
      }
    },
    components: {
      AddOrUpdate,
      goodsAdopt
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/business/goods/queryBusinessGoodsList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords,
            'lastStart': this.dataForm.lastStart,
            'lastEnd': this.dataForm.lastEnd,
            'goodStatues': this.dataForm.goodStatues,
            'goodType': this.dataForm.goodType
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
        // 未审核商品
        this.$http({
          url: this.$http.adornUrl('/business/goods/queryCount'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.num = data.data
          } else {
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 重新审核
      goodsAdopt (goodsId) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.goodsAdopt.init(goodsId)
        })
      },
      // 批量审核
      batch (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.goodsId
        })
        if (this.dataForm.batch === '') {
          this.$message('请选择审核类型')
          return false
        }
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '审核' : '批量审核'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/business/goods/modifyBatchIds/${this.dataForm.batch}`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.goodsid
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/goods/shoppingbusinessgoods/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 直接跳到未审核
      unaudited () {
        this.dataForm.goodStatues = 0
        this.getDataList()
      },
      // 上架
      issale (goodsid) {
        this.$confirm(`确定对该商品进行上架操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/goods/modifyIsSale'),
            method: 'post',
            data: this.$http.adornData(goodsid, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 下架
      noIssale (goodsid) {
        this.$confirm(`确定对该商品进行下架操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/goods/modifyNoIsSale'),
            method: 'post',
            data: this.$http.adornData(goodsid, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 通过
      adopt (goodsid) {
        this.$confirm(`确定对该商品通过?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/goods/modifyAdopt'),
            method: 'post',
            data: this.$http.adornData(goodsid, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 不通过
      noAdopt (goodsid) {
        this.$prompt('请输入不通过信息', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputType: 'textarea'
        }).then(({ value }) => {
          if (value === null || value === '') {
            this.$message.error('审核内容不能为空！')
            return false
          }
          this.$confirm(`确定对该商品不通过?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$http({
              url: this.$http.adornUrl(`/business/goods/modifyNoAdopt/${value}`),
              method: 'post',
              data: this.$http.adornData(goodsid, false)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
      }
    }
  }
</script>
